#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author: wangxiaoming 2021-11-16
# desc: thermal测试类

import enum
import unittest
import os
import time
import struct
from ctypes import *
from mainunit_common import *
from mainunit_api import *

class testself_led_drv(unittest.TestCase):
    g_err_val=0
    
    def error_callback(self, err):
        print(f'\terr={err}')
        self.g_err_val |= err
        
    def testself1_led_drv_color(self):
        print(f'\t==================================')
        expected=ECODE_OK
        valueset=c_int(0)
        status = c_int(-1)
        
        ret = leds_init()
        print(f'\tstep. call api: leds_init, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        txt = sp_input(f"\n\tPLEASE CHCECK LED COLOR AFTER PRESSING ANY KEY TO CONTINUE\n")        
        for i in range(8):
            valueset=c_int(i)
            ret = led_set_status(valueset)        
            print(f'\tstep. call api: led_set_status, input param={valueset} actual value={ret}, expected value={expected}')		
            self.assertEqual(ret, expected)
            time.sleep(1)
            status = c_int(-1)
            ret = led_get_status(pointer(status))        
            print(f'\tstep. call api: led_get_status, actual value={ret}, expected value={expected}')		
            self.assertEqual(ret, expected)
            self.assertEqual(i, status.value)
        led_list=[0x01, 0x02, 0x04]
        for j in led_list:
            for i in [0,25,50,75,100]:            
                led_type=j
                led_val=i<<8*(led_list.index(j)+1)
                valueset=c_int(led_val|led_type)            
                ret = led_set_status2(valueset,c_int(0))        
                print(f'\tstep. call api: led_set_status2, input param={hex(valueset.value)} actual value={ret}, expected value={expected}')		
                self.assertEqual(ret, expected)
                time.sleep(1)
                status = c_int(0)
                ret = led_get_status2(pointer(status))        
                print(f'\tstep. call api: led_get_status2, output param={hex(status.value)} actual value={ret}, expected value={expected}')
                self.assertEqual(ret, expected)
                #self.assertEqual(i, status.value)   
        ret = led_set_status(0)
        ret = leds_uinit()
        print(f'\tstep. call api: leds_uinit, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        print('\t**********************************')    
    def testself1_ecall_drv_color(self):
        print(f'\t==================================')
        expected=ECODE_OK
        valueset=c_int(0)
        status = c_int(-1)
        
        ret = leds_init()
        print(f'\tstep. call api: leds_init, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        txt = sp_input(f"\n\tPLEASE CHCECK ECALL LED COLOR AFTER PRESSING ANY KEY TO CONTINUE\n")        
        for i in [BSP_ON, BSP_OFF]:
            valueset=c_int(i)
            ret = ecall_led_set_status(valueset)        
            print(f'\tstep. call api: ecall_led_set_status, input param={valueset} actual value={ret}, expected value={expected}')		
            self.assertEqual(ret, expected)
            time.sleep(1)
            status = c_int(-1)
            ret = ecall_led_get_status(pointer(status))        
            print(f'\tstep. call api: ecall_led_get_status, actual value={ret}, expected value={expected}')		
            self.assertEqual(ret, expected)
            self.assertEqual(i, status.value)
        led_list=[0x01]
        for j in led_list:
            for i in [0,25,50,75,100]:            
                led_type=j
                led_val=i<<8*(led_list.index(j)+1)
                valueset=c_int(led_val|led_type)            
                ret = ecall_led_set_status2(valueset,c_int(0))        
                print(f'\tstep. call api: ecall_led_set_status2, input param={hex(valueset.value)} actual value={ret}, expected value={expected}')		
                self.assertEqual(ret, expected)
                time.sleep(1)
                status = c_int(0)
                ret = ecall_led_get_status2(pointer(status))        
                print(f'\tstep. call api: ecall_led_get_status2, output param={hex(status.value)} actual value={ret}, expected value={expected}')
                self.assertEqual(ret, expected)
                #self.assertEqual(i, status.value) 
        ret = ecall_led_set_status(0)
        ret = leds_uinit()
        print(f'\tstep. call api: leds_uinit, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        print('\t**********************************')    
    
    def testself1_irled_drv_color(self):
        print(f'\t==================================')
        expected=ECODE_OK
        expected_err=ECODE_LED_IRLED_DISCONNECT
        valueset=c_int(0)
        status = c_int(-1)
        erro_cb_obj = ErrorCallBack(self.error_callback)
        index=0
        
        #led init
        ret = leds_init()
        print(f'\tstep. call api: leds_init, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        txt = sp_input(f"\n\tPLEASE CHCECK IR LED COLOR AFTER PRESSING ANY KEY TO CONTINUE\n")        
        for i in [BSP_ON, BSP_OFF, BSP_ON, BSP_OFF]:
            #ir led setvalue
            valueset=c_int(i)
            ret = ir_led_set_status(valueset)        
            print(f'\tstep. call api: ir_led_set_status, input param={valueset.value} actual value={ret}, expected value={expected}')		
            self.assertEqual(ret, expected)
            #first on and off
            if index==0 or index==1:
                txt = sp_input(f"\n\tPLEASE CHECK WHETHER IR LED IS {'ON' if i==BSP_ON else 'OFF'} , {'ON' if i==BSP_ON else 'OFF'}, PLEASE ENTER 1\n")
                self.assertEqual(txt, '1')
                #ir led getvalue
                status = c_int(-1)
                ret = ir_led_get_status(pointer(status))        
                print(f'\tstep. call api: ir_led_get_status, actual value={ret}, expected value={expected}')		
                self.assertEqual(ret, expected)
                self.assertEqual(BSP_ON if i==BSP_ON else BSP_OFF, status.value)
            #second on
            elif index==2:
                #ir led diag
                ret = ir_led_diag(erro_cb_obj)
                print(f'\tstep. call api: ir_led_diag, actual value={ret}, expected value={expected}')		
                self.assertEqual(ret, expected)                   
            #second off
            else:
                txt = sp_input(f"\n\tPLEASE WAITE FOR SOME SECONDS FOR WAIT DIAG CALL BACK ANY KEY TO CONTINUE\n")                        
            index+=1
        #diag callback funciton return
        self.assertEqual(self.g_err_val, expected_err) 
        #led uinit
        ret = leds_uinit()
        print(f'\tstep. call api: leds_uinit, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        print('\t**********************************')    